//
// Created by wubh2 on 2023/3/10.
//

#ifndef BSP_JUDGE_H
#define BSP_JUDGE_H
#include "sys.h"

#define JUDGE_HEADER_AND_CMDID_LEN 7
#define JUDGE_CMDID_LEN 2       //命令码
#define JUDGE_HEADER_LEN 5      //帧头数据长度
#define JUDGE_CRC_LEN 2         //校验码
#define JUDGE_INTERACTIVE_HEADER_LEN 6

#define JUDGE_CMDID_LOW_INDEX 5
#define JUDGE_CMDID_HIGH_INDEX 6
#define JUDGE_LEN_LOW_INDEX 1
#define JUDGE_LEN_HIGH_INDEX 2

#define JUDGE_DN_REG_ID 0xA5

#define PROTOCAL_FRAME_MAX_SIZE  200
typedef struct
{
    u8 color;
    u8 sender;
    u8 reciver;
    u8 move_status;
    u8 uper_status;
    u8 lower_status;
    u8 transmit_send_status;
    u8 reactdata[5];
}Sentry_communication_struct;

/**
  * @brief  帧头定义
  */
#pragma pack (1)
typedef struct
{
    uint8_t  sof;							//固定值0xA5
    uint16_t data_length;					//数据段长度
    uint8_t  seq;							//包序号
    uint8_t  crc8;						//帧头校验
} frame_header_t;
#pragma pack ()


/*********************************************************************************
 * 														协议部分——与裁判系统通信
 *********************************************************************************/

#define JUDGE_FIFO_BUFLEN 500

/**
  * @brief  裁判系统命令码
  */
typedef enum
{
//从裁判系统接收
    GAME_STATE_ID        = 0x0001,		//比赛状态数据，1HZ
    GAME_RESULT_ID       = 0x0002,		//比赛结果数据，比赛结束后发送
    GAME_SURVIVORS_ID    = 0x0003,		//机器人血量数据，1HZ
    DART_STATUS_ID		 = 0X0004,		//飞镖发射状态,飞镖发射后发送
    EVENT_DATA_ID        = 0x0101,		//场地事件数据，事件改变后发送
    SUPPLY_ACTION_ID     = 0x0102,		//补给站动作标识，动作改变后发送
    SUPPLY_BOOKING_ID	 = 0x0103,		//补给站预约子弹，对抗赛未开启
    REFEREE_WARNING_ID	 = 0X0104,		//裁判警告信息,警告发生后发送
    DART_REMAINING_ID	 = 0X0105, 		//飞镖发射口倒计时,1Hz
    GAME_ROBOT_STATE_ID  = 0x0201,		//比赛机器人状态，10HZ
    POWER_HEAT_ID        = 0x0202,		//实时功率热量数据，50HZ
    ROBOT_POS_ID		 = 0x0203,	  //机器人位置，10HZ
    BUFF_MUSK_ID           = 0x0204,	  //机器人增益，状态改变后发送
    AERIAL_ROBOT_ENERGY_ID = 0x0205,	  //空中机器人能量状态
    ROBOT_HURT_ID          = 0x0206,	  //伤害状态，伤害发生后发送
    SHOOT_DATA_ID          = 0x0207,	  //实时射击信息，射击后发送
    BULLET_REMAIN_ID			 = 0X0208,		//子弹余量，1Hz 周期发送，空中机器人以及哨兵机器人主控发送
    RFID_STATUS_ID				 = 0X0209,		//机器人 RFID 状态1Hz，发送范围：单一机器人。
    DART_CMD_ID						 = 0X020A,		//飞镖机器人客户端指令数据：0x020A。发送频率：10Hz，发送范围：单一机器人。
    CUSTOM_CONTROLLER_ID	 = 0X0302,		//自定义控制器交互数据接口，客户端触发发送
    MINIMAP_DATA_ID				 = 0X0303,		//客户端小地图交互数据，触发发送
    KEYBORD_DATA_ID				 = 0X0304,		//键盘鼠标信息，图传串口发送

//向裁判系统发送 ，机器人间通信
    STU_INTERACTIVE_ID							=0x0301,     //交互数据接收信息
    STU_STU_DATA_ID									=0x0211,   //机器人间通信内容ID（0x0201~0x02FF)
    STU_CUSTOM_DELETE_PICTURE_ID		=0x0100,		//向客户端删除图形内容ID
    STU_CUSTOM_ONE_PICTURE_ID				=0x0101,		//向客户端一个图形内容ID
    STU_CUSTOM_TWO_PICTURE_ID				=0x0102,		//向客户端两个图形内容ID
    STU_CUSTOM_FIVE_PICTURE_ID			=0x0103,		//向客户端五个图形内容ID
    STU_CUSTOM_SEVEN_PICTURE_ID			=0x0104,		//向客户端七个图形内容ID
    STU_CUSTOM_CHARACTER_ID         =0x0110,    //向客户端字符内容ID
//通信ID确认
    RED_HERO_ID             = 0x0001,    //红方英雄机器人ID
    RED_ENGINEER_ID         = 0x0002,
    RED_INFANTRY3_ID        = 0x0003,
    RED_INFANTRY4_ID        = 0x0004,
    RED_INFANTRY5_ID        = 0x0005,
    RED_AERIAL_ID           = 0x0006,
    RED_SENTRY_ID           = 0x0007,
    RED_RADAR_ID            = 0x0009,
    BLUE_HERO_ID            = 0x0065,
    BLUE_ENGINEER_ID        = 0x0066,
    BLUE_INFANTRY3_ID       = 0x0067,
    BLUE_INFANTRY4_ID       = 0x0068,
    BLUE_INFANTRY5_ID       = 0x0069,
    BLUE_AERIAL_ID          = 0x006A,
    BLUE_SENTRY_ID          = 0x006B,
    BLUE_RADAR_ID           = 0x006D,
    RED_HERO_CUSTOM_ID      = 0x0101,    //红方英雄机器人客户端ID
    RED_ENGINEER_CUSTOM_ID  = 0x0102,
    RED_INFANTRY3_CUSTOM_ID = 0x0103,
    RED_INFANTRY4_CUSTOM_ID = 0x0104,
    RED_INFANTRY5_CUSTOM_ID = 0x0105,
    RED_AERIAL_CUSTOM_ID    = 0x0106,
    BLUE_HERO_CUSTOM_ID     = 0x0165,
    BLUE_ENGINEER_CUSTOM_ID = 0x0166,
    BLUE_INFANTRY3_CUSTOM_ID= 0x0167,
    BLUE_INFANTRY4_CUSTOM_ID= 0x0168,
    BLUE_INFANTRY5_CUSTOM_ID= 0x0169,
    BLUE_AERIAL_CUSTOM_ID   = 0x016A,

} judge_data_id_e;

/**
  * @brief  game information structures definition(0x0001)
  */
#pragma pack (1)
typedef struct
{
    uint8_t game_type : 4;
    uint8_t game_progress : 4;
    uint16_t stage_remain_time;
    uint64_t SyncTimeStamp;
} ext_game_state_t;

/**
  * @brief  game result(0x0002)
  */
typedef struct
{
    uint8_t winner;
} ext_game_result_t;

/**
  * @brief  (0x0003)
  */
typedef struct
{
    uint16_t red_1_robot_HP;
    uint16_t red_2_robot_HP;
    uint16_t red_3_robot_HP;
    uint16_t red_4_robot_HP;
    uint16_t red_5_robot_HP;
    uint16_t red_7_robot_HP;
    uint16_t red_outpost_HP;
    uint16_t red_base_HP;
    uint16_t blue_1_robot_HP;
    uint16_t blue_2_robot_HP;
    uint16_t blue_3_robot_HP;
    uint16_t blue_4_robot_HP;
    uint16_t blue_5_robot_HP;
    uint16_t blue_7_robot_HP;
    uint16_t blue_outpost_HP;
    uint16_t blue_base_HP;
} ext_game_robot_HP_t;

/**
  * @brief  (0x0004)
  */
typedef struct
{
    uint8_t dart_belong;
    uint16_t stage_remaining_time;
} ext_dart_status_t;

/**
  * @brief  (0x0101)
  */
typedef struct
{
    uint32_t event_type;
} ext_event_data_t;

/**
  * @brief  (0x0102)
  */
typedef struct
{
    uint8_t supply_projectile_id;
    uint8_t supply_robot_id;
    uint8_t supply_projectile_step;
    uint8_t supply_projectile_num;
} ext_supply_projectile_action_t;

/**
  * @brief  (0x0104)
  */
typedef struct
{
    uint8_t level;
    uint8_t foul_robot_id;
} ext_referee_warning_t;

/**
  * @brief  (0x0105)
  */
typedef struct
{
    uint8_t dart_remaining_time;
} ext_dart_remaining_time_t;

/**
  * @brief  (0x0201)
  */
typedef struct
{
    uint8_t robot_id;
    uint8_t robot_level;
    uint16_t remain_HP;
    uint16_t max_HP;
//    uint16_t shooter_id1_17mm_cooling_rate;
//    uint16_t shooter_id1_17mm_cooling_limit;
//    uint16_t shooter_id1_17mm_speed_limit;
//    uint16_t shooter_id2_17mm_cooling_rate;
//    uint16_t shooter_id2_17mm_cooling_limit;
//    uint16_t shooter_id2_17mm_speed_limit;
    uint16_t shooter_id1_42mm_cooling_rate;
    uint16_t shooter_id1_42mm_cooling_limit;
    ///16m/s
//    uint16_t shooter_id1_42mm_speed_limit;
    uint16_t chassis_power_limit;
    uint8_t mains_power_gimbal_output : 1;
    uint8_t mains_power_chassis_output : 1;
    uint8_t mains_power_shooter_output : 1;
} ext_game_robot_status_t;

/**
  * @brief  (0x0202)
  */
typedef struct
{
    uint16_t chassis_volt;
    uint16_t chassis_current;
    float chassis_power;
    //缓冲区能量
    uint16_t chassis_power_buffer;
    uint16_t shooter_id1_17mm_cooling_heat;
    uint16_t shooter_id2_17mm_cooling_heat;
    uint16_t shooter_id1_42mm_cooling_heat;
} ext_power_heat_data_t;

/**
  * @brief  (0x0203)
  */
typedef struct
{
    float x;
    float y;
    float z;
    float yaw;
} ext_game_robot_pos_t;

/**
  * @brief  (0x0204)
  */
typedef struct
{
    uint8_t power_rune_buff;
}ext_buff_musk_t;

/**
  * @brief  (0x0205)
  */
typedef struct
{
    uint8_t attack_time;
} aerial_robot_energy_t;

/**
  * @brief  (0x0206)
  */
typedef struct
{
    uint8_t armor_id : 4;
    uint8_t hurt_type : 4;
} ext_robot_hurt_t;

/**
  * @brief  (0x0207)
  */
typedef struct
{
    uint8_t bullet_type;
    uint8_t shooter_id;
    uint8_t bullet_freq;
    float bullet_speed;
} ext_shoot_data_t;

/**
  * @brief  (0x0208)
  */
typedef struct
{
    uint16_t bullet_remaining_num_17mm;
    uint16_t bullet_remaining_num_42mm;
    uint16_t coin_remaining_num;
} ext_bullet_remaining_t;

/**
  * @brief  (0x0209)
  */
typedef struct
{
    uint32_t rfid_status;
} ext_rfid_status_t;

/**
  * @brief  (0x020A)
  */
typedef struct
{
    uint8_t dart_launch_opening_status;
    uint8_t dart_attack_target;
    uint16_t target_change_time;
    uint8_t first_dart_speed;
    uint8_t second_dart_speed;
    uint8_t third_dart_speed;
    uint8_t fourth_dart_speed;
    uint16_t last_dart_launch_time;
    uint16_t operate_launch_cmd_time;
} ext_dart_client_cmd_t;

/**
  * @brief  student custom data(0x0301)
  */
typedef struct
{
    uint16_t data_cmd_id;
    uint16_t send_ID;
    uint16_t receiver_ID;
}ext_student_interactive_header_data_t;


/**
  * @brief  student student data(0x0301)---(0x0201~0x02FF)
学生机器人间通信
  */
typedef  struct
{
    uint8_t data[16];//字节大小小于113
} robot_interactive_data_t;

/**
  * @brief  student student data conjunction
  */
typedef  struct
{
    ext_student_interactive_header_data_t   student_interactive_header_data;
    robot_interactive_data_t                robot_interactive_data;
}ext_student_student_data_t;
/**
  * @brief  student custom delete picture(0x0301)---(0x0100)
  */
typedef  struct
{
    uint8_t operate_type;//0 空操作 1 删除图层 2 删除所有
    uint8_t layer;//图层数0-9
}ext_client_custom_graphic_delete_t;
/**
  * @brief  student custom graph data
  */
typedef struct
{
    uint8_t graphic_name[3];//图形名
    uint32_t operate_type:3;//图形操作: 1:增加 2:修改 3:删除
    uint32_t graphic_type:3;//图形类型 0:直线 1:矩形 2:圆 3:椭圆 4:圆弧 5:浮点数 6:整形 7:字符
    uint32_t layer:4;//图层数0-9
    uint32_t color:4;//颜色 0:红蓝主色 1:黄色 2:绿色 3:橙色 4:紫红色 5:粉 6:青 7:黑 8:白
    uint32_t details_a:9;
    uint32_t details_b:9;//终止角度
    uint32_t width:10;
    uint32_t start_x:11;
    uint32_t start_y:11;
    uint32_t details_c:10;//字体大小或半径
    uint32_t details_d:11;
    uint32_t details_e:11;
} graphic_data_struct_t;
//※一个图形，一个图形。见通信协议25页

/**
  * @brief  student custom draw one picture(0x0301)---(0x0101)
  */
typedef  struct
{
    graphic_data_struct_t grapic_data_struct;
}ext_client_custom_graphic_single_t;
/**
  * @brief  student custom draw two picture(0x0301)---(0x0102)
  */
typedef  struct
{
    graphic_data_struct_t grapic_data_struct[2];
} ext_client_custom_graphic_double_t;
/**
  * @brief  student custom draw five picture(0x0301)---(0x0103)
  */
typedef  struct
{
    graphic_data_struct_t grapic_data_struct[5];
} ext_client_custom_graphic_five_t;
/**
  * @brief  student custom draw seven picture(0x0301)---(0x0104)
  */
typedef  struct
{
    graphic_data_struct_t grapic_data_struct[7];
} ext_client_custom_graphic_seven_t;

typedef  struct
{
    graphic_data_struct_t grapic_data_struct;
    uint8_t data[30];
} ext_client_custom_character_t;//字符型
/**
  * @brief costum_interactive_data_t(0x0302)
  */

typedef  struct
{
    uint8_t data[30];
} ext_custum_interactive_data_t;
/**
  * @brief Interactive information of small map(0x0303)
  */
typedef  struct
{
    float target_position_x;
    float target_position_y;
    float target_position_z;
    uint8_t commd_keyboard;
    uint16_t target_robot_ID;
} ext_minimap_t;
/**
  * @briefPicture transmission remote control information(0x0304)
  */
typedef  struct
{
    int16_t mouse_x;
    int16_t mouse_y;
    int16_t mouse_z;
    int8_t left_button_down;
    int8_t right_button_down;
    uint16_t keyboard_value;
    uint16_t reserved;
} ext_interactive_information_t;
#pragma pack ()
/**
  * @brief  the data structure receive from judgement
  */
typedef struct
{
    ext_game_state_t                    game_state_data;               	//0x0001
    ext_game_result_t                   game_result_data;              	//0x0002
    ext_game_robot_HP_t            		ext_game_robot_HP_t;     		//0x0003
    ext_dart_status_t					dart_status_data;				//0x0004
    ext_event_data_t                    event_data;                    	//0x0101
    ext_supply_projectile_action_t      supply_projectile_action_data; 	//0x0102
    ext_referee_warning_t				referee_warning_data;			//0x0104
    ext_dart_remaining_time_t			dart_remaining_data;			//0x0105
    ext_game_robot_status_t             game_robot_state_data;         	//0x0201  1:1Byte+3:2Byte
    ext_power_heat_data_t               power_heat_data;               	//0x0202
    ext_game_robot_pos_t                game_robot_pos_data;           	//0x0203
    ext_buff_musk_t                     buff_musk_data;                	//0x0204
    aerial_robot_energy_t               aerial_robot_energy_data;      	//0x0205
    ext_robot_hurt_t                    robot_hurt_data;               	//0x0206
    ext_shoot_data_t                    shoot_data;                    	//0x0207
    ext_student_student_data_t          student_student_data;
    ext_bullet_remaining_t				bullet_reamining_data;			//0x0208
    ext_rfid_status_t					rfid_status_data;				//0x0209
    ext_dart_client_cmd_t				dart_cmd_data;					//0x020A
    ext_custum_interactive_data_t		custom_interactive_data;		//0x0302
    ext_minimap_t						mini_map_data;					//0x0303
    ext_interactive_information_t		interactive_information_data;  	//0x0304
} receive_judge_t;

extern receive_judge_t judge_rece_mesg;
void judgeDataHandle(uint8_t *p_frame);
void data_packet_pack(uint16_t cmd_id, uint8_t* p_data, uint16_t len, ext_student_interactive_header_data_t student_interactive_header);
void StuInteractiveData(void);
#endif //BSP_JUDGE_H
